<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-点击修改元素背景颜色</title>
    <style>
        .container{
            width: 400px;
            height: 300px;
            background: #aef;
            position: absolute;
            left: 0;
            top: 0;
            bottom:0;
            right: 0;
            margin: auto;
        }
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

    <script type="text/babel">
        //解构赋值操作 
        let {random} = _;
        let rand = _.random;

        class App extends React.Component{
            state = {
                bg: 'rgb(0,0,0)'
            }

            render(){
                return <div className="container" style={{background: this.state.bg}} onClick={this.changeBg}></div>;
            }

            changeBg = () => {
                this.setState({
                    bg: `rgb(${rand(0, 255)}, ${rand(0, 255)}, ${rand(0, 255)})`
                })
            }
        }

        //渲染
        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>